﻿using Microsoft.Xna.Framework.Input;

namespace Phoenix.Xna.Framework.Input.Mice
{
    /// <summary>
    /// Extensions to the Phoenix.Xna.Framework.Input.Mice.Buttons object.
    /// </summary>
    public static class ButtonsExtensions
    {
        /// <summary>
        /// Determines if the button is up.
        /// </summary>
        /// <param name="button">The extended object.</param>
        /// <returns>True if up, otherwise false.</returns>
        public static bool IsButtonUp(this Buttons button)
        {
            return (button.ResolveButtonState(MouseStateManager.Instance.CurrentState) == ButtonState.Released) ? true : false;
        }

        /// <summary>
        /// Determines if the button was up.
        /// </summary>
        /// <param name="button">The extended object.</param>
        /// <returns>True if up, otherwise false.</returns>
        public static bool WasButtonUp(this Buttons button)
        {
            return (button.ResolveButtonState(MouseStateManager.Instance.PreviousState) == ButtonState.Released) ? true : false;
        }

        /// <summary>
        /// Determines if the button is down.
        /// </summary>
        /// <param name="button">The extended object.</param>
        /// <returns>True if down, otherwise false.</returns>
        public static bool IsButtonDown(this Buttons button)
        {
            return (button.ResolveButtonState(MouseStateManager.Instance.CurrentState) == ButtonState.Pressed) ? true : false;
        }

        /// <summary>
        /// Determines if the button was down.
        /// </summary>
        /// <param name="button">The extended object.</param>
        /// <returns>True if down, otherwise false.</returns>
        public static bool WasButtonDown(this Buttons button)
        {
            return (button.ResolveButtonState(MouseStateManager.Instance.PreviousState) == ButtonState.Pressed) ? true : false;
        }

        /// <summary>
        /// Determines if the button was pressed.
        /// </summary>
        /// <param name="button">The extended object.</param>
        /// <returns>True if pressed, otherwise false.</returns>
        public static bool WasButtonPressed(this Buttons button)
        {
            return (button.WasButtonUp() && button.IsButtonDown()) ? true : false;
        }

        /// <summary>
        /// Determines if the button was released.
        /// </summary>
        /// <param name="button">The extended object.</param>
        /// <returns>True if released, otherwise false.</returns>
        public static bool WasButtonReleased(this Buttons button)
        {
            return (button.WasButtonDown() && button.IsButtonUp()) ? true : false;
        }

        /// <summary>
        /// Determines if the button was held.
        /// </summary>
        /// <param name="button">The extended object.</param>
        /// <returns>True if held, otherwise false.</returns>
        public static bool WasButtonHeld(this Buttons button)
        {
            return (button.WasButtonDown() && button.IsButtonDown()) ? true : false;
        }

        /// <summary>
        /// Resolves which button state to get from the supplied MouseState object.
        /// </summary>
        /// <param name="button">The button to resolve the state for.</param>
        /// <param name="mouseState">The state object to get the button state from.</param>
        /// <returns>The button's state.</returns>
        private static ButtonState ResolveButtonState(this Buttons button, MouseState mouseState)
        {
            switch (button)
            {
                case Buttons.LeftButton:
                    return mouseState.LeftButton;
                case Buttons.MiddleButton:
                    return mouseState.MiddleButton;
                case Buttons.RightButton:
                    return mouseState.RightButton;
                case Buttons.XButton1:
                    return mouseState.XButton1;
                case Buttons.XButton2:
                    return mouseState.XButton2;
                default:
                    return ButtonState.Released;
            }
        }
    }
}
